﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Data.Linq;
using System.Linq;
using Microsoft.Phone.Shell;

namespace PostIt
{
    public class MainViewModel : INotifyPropertyChanged
    {
        private TasksDBContext dbDataContext;

        public MainViewModel()
        {
            dbDataContext = App.DB;
            this.Items = new ObservableCollection<ItemViewModel>();
            
        }


        /// <summary>
        /// A collection for ItemViewModel objects.
        /// </summary>
        public ObservableCollection<ItemViewModel> Items { get; private set; }



        private string _itemsProperty = "Items Runtime Property Value";
        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding
        /// </summary>
        /// <returns></returns>
        public string ItemsProperty
        {
            get
            {
                return _itemsProperty;
            }
            set
            {
                if (value != _itemsProperty)
                {
                    _itemsProperty = value;
                    NotifyPropertyChanged("ItemsProperty");
                }
            }
        }

        public bool IsDataLoaded
        {
            get;
            private set;
        }


        /// <summary>
        /// Creates and adds a few ItemViewModel objects into the Items collection.
        /// </summary>
        public void LoadData()
        {

          //  checkDeletedTiles();


            var tasksDB = from Tasks s in dbDataContext.Items
                              select s ;
       
            // Query the database and load all to-do items.
       var allTask = new ObservableCollection<Tasks>(tasksDB.OrderBy(c => c.isDone).ThenBy(n => n.DueDate));

            foreach (Tasks aTask in allTask)
            {
                //
                // Create an empty ItemViewModel
                //
                ItemViewModel ivm = new ItemViewModel();

                //
                // The main item shown in the ListBox is the number
                // of the sonnet in Roman numerals
                //
                ivm.TaskId = aTask.TaskId;
                ivm.Title = aTask.Title;
                if (aTask.isDone)
                {
                    ivm.rectOpa = 0.60;
                    ivm.isDoneImgAdr = "/icons/Done.png";
                }
                else {

                    ivm.rectOpa = 0.0;
                    ivm.isDoneImgAdr = "/icons/notDone.png";
                
                }
                if (aTask.isPin)
                {
               
                    ivm.isPinImgAdr = "/icons/notPin.png";
                }
                else
                {

                   
                    ivm.isPinImgAdr = "/icons/pin.png";

                }



                ivm.tileText = aTask.tileText;

                ivm.Detail = aTask.Detail;
                ivm.isPin= aTask.isPin.ToString();
                ivm.DueDate= aTask.DueDate.ToShortDateString();

                ivm.CreationDate = aTask.CreationDate;
                ivm.BackColor = aTask.backColor;
                ivm.isDone = aTask.isDone.ToString();
                Debug.WriteLine(ivm.isDone);

                //
                // And finally, we add it to the Items collection used
                // as a DataBinding source for ListBox
                this.Items.Add(ivm);

             

            }

            // Sample data; replace with real data
            //this.Items.Add(new ItemViewModel() { LineOne = "runtime one", LineTwo = "Maecenas praesent accumsan bibendum", LineThree = "Facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu" });
            //this.Items.Add(new ItemViewModel() { LineOne = "runtime two", LineTwo = "Dictumst eleifend facilisi faucibus", LineThree = "Suscipit torquent ultrices vehicula volutpat maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus" });


            this.IsDataLoaded = true;
        }
        private ShellTile FindTile(string partOfUri)
        {
            ShellTile shellTile = ShellTile.ActiveTiles.FirstOrDefault(
                tile => tile.NavigationUri.ToString() == "/DetailsPage.xaml?" + partOfUri);

            return shellTile;
        }
      
        
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            if (null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}